package com.acce.gxsc.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import com.acce.gxsc.R;
import com.acce.gxsc.page.inCarRecord.InCarRecordFragment;
import com.acce.gxsc.page.outCar.OutCarFragment;
import com.acce.gxsc.page.outCarHandover.OutCarHandoverFragment;
import com.acce.gxsc.page.registerCar.RegisterCarFragment;
import com.acce.gxsc.page.registerCarRecord.RegisterCarRecordFragment;
import com.acce.gxsc.page.reportForm.ReportFormFragment;
import com.acce.gxsc.page.supervision.SupervisionFragment;
import com.acce.gxsc.page.supervisionReport.list.SupervisionReportListFragment;
import com.acce.sdk.BaseActivity;
import com.acce.sdk.base.main.Category;
import com.acce.sdk.base.main.TextSharedElementCallback;
import com.acce.sdk.widget.AcceBar;
import com.acce.sdk.widget.materialSearchView.MaterialSearchView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangjian on 2016/10/18.
 */

public class QuizActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    public AcceBar toolbar;
    @Bind(R.id.fg_container)
    FrameLayout fgContainer;
    @Bind(R.id.msv_search_view)
    public MaterialSearchView msvSearchView;

    private Category mCategory;
    private Fragment mFragment;
    private String   fragmentTag;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        ButterKnife.bind(this);
        initData();
        initLayoutView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fg_container);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mCategory = Constants.categoryList.get(bundle.getInt(Constants.Key.CATEGORY_ID));
        }
//        App.currentThemeId = mCategory.getTheme().getStyleId();
//        App.toolbarTextColor = ContextCompat.getColor(this,
//                mCategory.getTheme().getTextPrimaryColor());
    }

    private void initLayoutView() {
//        setTheme(App.currentThemeId);
//        if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
//            Window window = getWindow();
//            window.setStatusBarColor(ContextCompat.getColor(this,
//                    mCategory.getTheme().getPrimaryDarkColor()));
//        }
        setContentView(R.layout.activity_quiz);
        ButterKnife.bind(this);
        fgContainer.setBackgroundColor(ContextCompat.
                getColor(this, R.color.divider_dark));
        toolbar.setTitleText(mCategory.getName());
        toolbar.setPageCode("AS1020");
        int categoryNameTextSize = getResources()
                .getDimensionPixelSize(R.dimen.category_item_text_size);
        int paddingStart = getResources().getDimensionPixelSize(R.dimen.spacing_double);
        final int startDelay = getResources().getInteger(R.integer.toolbar_transition_duration);

        ActivityCompat.setEnterSharedElementCallback(this,
                new TextSharedElementCallback(categoryNameTextSize, paddingStart) {
                    @Override
                    public void onSharedElementStart(List<String> sharedElementNames,
                                                     List<View> sharedElements,
                                                     List<View> sharedElementSnapshots) {
                        super.onSharedElementStart(sharedElementNames,
                                sharedElements,
                                sharedElementSnapshots);
                        toolbar.getPressBackView().setScaleX(0f);
                        toolbar.getPressBackView().setScaleY(0f);
                        toolbar.getManagementView().setScaleX(0f);
                        toolbar.getManagementView().setScaleY(0f);
                    }

                    @Override
                    public void onSharedElementEnd(List<String> sharedElementNames,
                                                   List<View> sharedElements,
                                                   List<View> sharedElementSnapshots) {
                        super.onSharedElementEnd(sharedElementNames,
                                sharedElements,
                                sharedElementSnapshots);
                        // Make sure to perform this animation after the transition has ended.
                        ViewCompat.animate(toolbar.getPressBackView()).setStartDelay(startDelay).scaleX(1f).scaleY(1f).alpha(1f).start();
                        ViewCompat.animate(toolbar.getManagementView()).setStartDelay(startDelay).scaleX(1f).scaleY(1f).alpha(1f).start();
                    }
                });

        findFragment();
        initFragment();
    }

    private void findFragment() {
        switch (mCategory.getId()) {
            case Constants.ID_0:
                newFragmentAc0();
                break;
            case Constants.ID_1:
                newFragmentAc1();
                break;
            case Constants.ID_2:
                newFragmentAc2();
                break;
            case Constants.ID_3:
                newFragmentAc3();
                break;
            case Constants.ID_4:
                newFragmentAc4();
                break;
            case Constants.ID_5:
                newFragmentAc5();
                break;
            case Constants.ID_6:
                newFragmentAc6();
                break;
            case Constants.ID_7:
                newFragmentAc7();
                break;
            default:
                break;
        }
    }

    private void newFragmentAc7() {
        fragmentTag = "id_7";
        mFragment = SupervisionReportListFragment.newInstance();
        toolbar.setSearchVisible(false);
    }

    private void newFragmentAc6() {
        fragmentTag = "id_6";
        mFragment = SupervisionFragment.newInstance();
        toolbar.setSearchVisible(false);
    }

    private void newFragmentAc5() {
        fragmentTag = "id_5";
        mFragment = OutCarHandoverFragment.newInstance();
        toolbar.setSearchVisible(false);
    }

    private void newFragmentAc4() {
        fragmentTag = "id_4";
        mFragment = ReportFormFragment.newInstance();
        toolbar.setSearchVisible(false);
    }

    private void newFragmentAc3() {
        fragmentTag = "id_3";
        mFragment = OutCarFragment.newInstance();
        toolbar.setSearchVisible(false);
    }

    private void newFragmentAc2() {
        boolean isSelect = false;
        if (getIntent().hasExtra(Constants.Key.IS_SELECT)) {
            isSelect = getIntent().getBooleanExtra(Constants.Key.IS_SELECT, false);
        }
        fragmentTag = "id_2";
        mFragment = RegisterCarRecordFragment.newInstance(isSelect);
        toolbar.setSearchVisible(true);
        toolbar.setSearchClickListener(new AcceBar.OnSearchListener() {
            @Override
            public void OnSearchClick() {
                msvSearchView.openSearch();
                msvSearchView.setQuery( ((RegisterCarRecordFragment)mFragment).getNo(), false);
            }
        });
        msvSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                ((RegisterCarRecordFragment)mFragment).find(query);
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        msvSearchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Do something when the suggestion list is clicked.
                String suggestion = msvSearchView.getSuggestionAtPosition(position);
                msvSearchView.setQuery(suggestion, true);
            }
        });
    }

    private void newFragmentAc1() {
        fragmentTag = "id_1";
        mFragment = InCarRecordFragment.newInstance();
        toolbar.setSearchVisible(false);
    }

    public void newFragmentAc0() {
        fragmentTag = "id_0";
        String pregisterKey = null;
        if (getIntent().hasExtra("pregisterKey")) {
            pregisterKey = getIntent().getStringExtra("pregisterKey");
        }
        mFragment = RegisterCarFragment.newInstance(pregisterKey);
        toolbar.setSearchVisible(false);
    }

    private void initFragment(){
        if (mFragment != null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fg_container, mFragment, fragmentTag)
                    .commit();
        }
    }
}
